package com.scs.application.modules.msg;

import com.scs.application.core.msg.domain.SceneMessage;
import com.scs.application.core.msg.enums.ReceiverType;
import com.scs.application.modules.msg.handler.MessageQueue;
import com.scs.application.modules.msg.handler.MessageQueueSingle;
import com.scs.application.modules.msg.ram.StaticMessageQueue;
import com.scs.application.modules.msg.ram.LesonQueueBuilder;
import com.scs.application.modules.msg.ram.demo.Message;
import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @description:
 */
public class MyQueue {

    @Test
    public void test1(){
        String queueName = "leisq";
        LesonQueueBuilder queue = LesonQueueBuilder.builder(Message.class, queueName, LesonQueueBuilder.save);
        Message o = (Message)queue.get();
        System.out.println(o.toString());

    }

    @Test
    @Deprecated
    public void test2(){
        Map<String, Message> a = new ConcurrentHashMap<String, Message>();
        Message m1 = new Message();
        m1.setFrom("魄罗");
        m1.setTo("布隆");
        m1.setMessage("给我带块小饼干！");
        a.put("leisq1",m1);

        Message m2 = new Message();
        m2.setFrom("布隆");
        m2.setTo("魄罗");
        m2.setMessage("收到！");
        a.put("leisq2",m2);

    }

    @Test
    public void test3_putMessage() {
        SceneMessage msg = new SceneMessage();
        msg.setBizKey("bizkey");
        msg.setBizType("类型");
        msg.setParams(null);
        msg.setReceiver("leisq");
        msg.setReceiverType(ReceiverType.ALL);
        msg.setSceneCode("1234");
//        MessageQueue.put(msg);
        MessageQueueSingle.builder().put(msg);

    }

    @Test
    public void test4_getMessage(){
//        SceneMessage msg = MessageQueue.get();
        Object msg = MessageQueueSingle.builder().get();


        System.out.println(msg);

    }

    /**
     * 测转换异常
     * @throws InterruptedException
     */
    @Test
    public void test5() throws InterruptedException {

        String path = "E:/SVN/mscm3/target/test-classes/MQ/test.lmq";
        BlockingQueue<SceneMessage> store = new LinkedBlockingQueue<SceneMessage>();
        SceneMessage msg = new SceneMessage();
        msg.setBizKey("bizkey");
        msg.setBizType("类型");
        msg.setParams(null);
        msg.setReceiver("leisq");
        msg.setReceiverType(ReceiverType.ALL);
        msg.setSceneCode("1234");
        store.put(msg);

        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream(path);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(store);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (fos != null)
                    fos.close();
                if (oos != null)
                    oos.close();
            } catch (Exception ex) {
            }
        }

        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream(path);
            ois = new ObjectInputStream(fis);
            BlockingQueue<SceneMessage> s = (LinkedBlockingQueue<SceneMessage>) ois.readObject();
            System.out.println(s.toString());
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (fis != null)
                    fis.close();
                if (ois != null)
                    ois.close();
            } catch (Exception ex) {
            }
        }

    }

    /**
     * 静态来测试
     */
    @Test
    public void test6(){
        SceneMessage[] array = MessageQueue.array();
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i].toString());
        }
    }

    /**
     * 静态来测试
     */
    @Test
    public void test7(){
        SceneMessage msg = new SceneMessage();
        msg.setBizKey("bizkey");
        msg.setBizType("类型");
        msg.setParams(null);
        msg.setReceiver("leisq");
        msg.setReceiverType(ReceiverType.ALL);
        msg.setSceneCode("1234");
        StaticMessageQueue.put(msg);

//        StaticMessageQueue.array(SceneMessage.class);
    }

    public static void main(String[] args) {
        // 生产
        String queueName = "leisq";
        LesonQueueBuilder queue = LesonQueueBuilder.builder(Message.class, queueName, LesonQueueBuilder.save);

        Message m1 = new Message();
        m1.setFrom("魄罗");
        m1.setTo("布隆");
        m1.setMessage("给我带块小饼干！");
        queue.put(m1);

        Message m2 = new Message();
        m2.setFrom("布隆");
        m2.setTo("魄罗");
        m2.setMessage("收到！");
        queue.put(m2);

        Iterator i = queue.iterable();
        while (i.hasNext()){
            Message m = (Message)i.next();
            i.remove();
            System.out.println(m.toString());
        }
    }
}
